package p500.srm513;

import utils.ExampleRunner;

/**
 * Created by IntelliJ IDEA.
 * User: smalex
 * Date: 7/26/11
 * Time: 3:37 PM
 */
public class CutTheNumbers {
  public int maximumSum(String[] board) {
    int res = horizontal(board);
    final int res2 = vertical(board);
    return Math.max(res, res2);
  }

  private int vertical(String[] board) {
    int res = 0;
    final int H = board.length;
    final int W = board[0].length();
    for (int x = 0; x < W; x++) {
      int sum = 0;
      for (int y = 0; y < H; y++) {
        sum *= 10;
        sum += board[y].charAt(x) - '0';
      }
      res += sum;
    }
    return res;
  }

  private int horizontal(String[] board) {
    int res = 0;
    final int H = board.length;
    final int W = board[0].length();
    for (int y = 0; y < H; y++) {
      int sum = 0;
      for (int x = 0; x < W; x++) {
        sum *= 10;
        sum += board[y].charAt(x) - '0';
      }
      res += sum;
    }
    return res;
  }

  public static void main(String[] args) {
    ExampleRunner.eq(0, 435, new CutTheNumbers().maximumSum(new String[]{"123", "312"}));
  }
}
